package com.yunshipei.core.utils;

import com.yunshipei.core.common.Callback;
import com.yunshipei.core.manager.LogManager;
import com.yunshipei.core.manager.XCloud2Map;

import org.json.JSONObject;

import java.util.List;

public class YspLogUtils {

    public static void d(String log) {
        LogManager.getInstance().saveDebugLog(log);
    }

    private static void nativeLog(String level, String action, JSONObject content, int status, String scope, String rtVersion) {
        LogManager.getInstance().saveNativeLog(level, action, content, status, scope, rtVersion);
    }

    //适配包下载出错
    public static void adapterFileDownloadError(String url, String message, String scope, String rtVersion) {
        downloadResError("downloadAdapterFile", url, message, scope, rtVersion);
    }

    //运行时下载出错
    public static void rtFileDownloadError(String url, String message, String scope, String rtVersion) {
        downloadResError("downloadRuntimeFile", url, message, scope, rtVersion);
    }

    private static void downloadResError(String action, String url, String message, String scope, String rtVersion) {
        List<String> logs = XCloud2Map.getInstance().getConfigInfo().getLogs();
        if (logs.contains("error")) {
            try {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("url", url);
                jsonObject.put("message", message);
                nativeLog("error", action, jsonObject, LogManager.STATUS_END, scope, rtVersion);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    //开始下载适配包
    public static void adapterFileDownloadStart(String url, String scope, String rtVersion) {
        clientDownload("downloadAdapterFile", url, scope, rtVersion, LogManager.STATUS_START);
    }

    //结束下载适配包
    public static void adapterFileDownloadEnd(String url, String scope, String rtVersion) {
        clientDownload("downloadAdapterFile", url, scope, rtVersion, LogManager.STATUS_END);
    }

    //开始下载运行时
    public static void rtFileDownloadStart(String url, String scope, String rtVersion) {
        clientDownload("downloadRuntimeFile", url, scope, rtVersion, LogManager.STATUS_START);
    }

    //结束下载运行时
    public static void rtFileDownloadEnd(String url, String scope, String rtVersion) {
        clientDownload("downloadRuntimeFile", url, scope, rtVersion, LogManager.STATUS_END);
    }

    private static void clientDownload(String action, String url, String scope, String rtVersion, int status) {
        List<String> logs = XCloud2Map.getInstance().getConfigInfo().getLogs();
        if (logs.contains("debug")) {
            try {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("url", url);
                nativeLog("debug", action, jsonObject, status, scope, rtVersion);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    //本地资源开始加载时间
    public static void localResStart(String url, String scope, String rtVersion) {
        reqRes("localResource", url, scope, rtVersion, LogManager.STATUS_START);
    }

    //本地资源读取完毕
    public static void localResEnd(String url, String scope, String rtVersion, String length) {
        reqRes("localResource", url, scope, rtVersion, LogManager.STATUS_END, length);
    }

    //网络资源开始加载
    public static void netResStart(String url, String scope, String rtVersion) {
        reqRes("networkResource", url, scope, rtVersion, LogManager.STATUS_START);
    }

    //网络资源结束加载
    public static void netResEnd(String url, String scope, String rtVersion, String length) {
        reqRes("networkResource", url, scope, rtVersion, LogManager.STATUS_END, length);
    }

    private static void reqRes(String resourceType, String url, String scope, String rtVersion, int status) {
        List<String> logs = XCloud2Map.getInstance().getConfigInfo().getLogs();
        if (logs.contains("debug")) {
            try {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("url", url);
                jsonObject.put("resourceType", resourceType);
                nativeLog("debug", "requestResources", jsonObject, status, scope, rtVersion);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private static void reqRes(String resourceType, String url, String scope, String rtVersion, int status, String length) {
        List<String> logs = XCloud2Map.getInstance().getConfigInfo().getLogs();
        if (logs.contains("debug")) {
            try {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("url", url);
                jsonObject.put("resourceType", resourceType);
                jsonObject.put("contentLength", length);
                nativeLog("debug", "requestResources", jsonObject, status, scope, rtVersion);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    //开始匹配
    public static void matchStart(String url) {
        match(url, LogManager.STATUS_START);
    }

    //结束匹配
    public static void matchEnd(String url) {
        match(url, LogManager.STATUS_END);
    }

    private static void match(String url, int status) {
        List<String> logs = XCloud2Map.getInstance().getConfigInfo().getLogs();
        if (logs.contains("debug")) {
            try {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("url", url);
                nativeLog("debug", "match", jsonObject, status, "", "");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static void adapterLog(String log) {
        LogManager.getInstance().saveAdapterLog(log);
    }

    public static void uploadLog(String fileName) {
        LogManager.getInstance().sendLog(fileName, null);
    }

    public static void uploadLog(String fileName, Callback callback) {
        LogManager.getInstance().sendLog(fileName, callback);
    }
}
